import { fakeRegister } from '@/services/api';
import { setAuthority } from '@/utils/authority';
import { reloadAuthorized } from '@/utils/Authorized';

export default {
    namespace: 'register',

    state: {
        status: undefined,
        msg: ''
    },

    effects: {
        *submit({ payload }, { call, put }) {
            const response = yield call(fakeRegister, payload);
            yield put({
                type: 'registerHandle',
                payload: response
            });
        }
    },

    reducers: {
        registerHandle(state, { payload }) {
            setAuthority('user');
            reloadAuthorized();
            return {
                ...state,
                status: payload.success ? 'ok' : 'error',
                msg: payload.msg
            };
        },
        clear(state) {
            return {
                ...state,
                status: undefined,
                msg: ''
            };
        }
    }
};
